package com.example.service;

import com.example.entity.ProductCategory;
import com.example.mapper.ProductCategoryMapper;
import com.example.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper categoryMapper;

    @Override
    @Transactional
    public ProductCategory createCategory(ProductCategory category) {
        if (category.getStatus() == null) {
            category.setStatus(1);
        }
        if (category.getSortOrder() == null) {
            category.setSortOrder(0);
        }
        categoryMapper.insert(category);
        return category;
    }

    @Override
    @Transactional
    public boolean deleteCategory(Long id) {
        return categoryMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public ProductCategory updateCategory(ProductCategory category) {
        categoryMapper.update(category);
        return categoryMapper.selectById(category.getId());
    }

    @Override
    public ProductCategory getCategoryById(Long id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public List<ProductCategory> getAllCategories() {
        return categoryMapper.selectAll();
    }

    @Override
    public ProductCategory getCategoryByName(String name) {
        return categoryMapper.selectByName(name);
    }
}

